package com.wctj.api.infra.cmb;

import com.wctj.api.base.utils.JacksonUtil;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 招商银行银企直联国密免前置/SaaS对接示例，本示例仅供参考，不保证各种异常场景运行，请勿直接使用，如有错漏请联系对接人员。运行时，请使用所获取的测试资源替换 用户编号、公私钥、对称密钥、服务商编号等信息。
 *
 * @author cmb.firmbank
 * @date 2023/7/20
 */
public class ApiDemo {

    private static final int BOUND_START = 1000000;
    private static final int BOUND_END = 9000000;
    // 测试地址，生产需要替换
    private static String url = "http://cdctest.cmburl.cn:80/cdcserver/api/v2";
    // 生产地址
    //private static String url = "https://cdc.cmbchina.com/cdcserver/api/v2";
    // 银行公钥，生产需要替换
    private static String publicKey = "BNsIe9U0x8IeSe4h/dxUzVEz9pie0hDSfMRINRXc7s1UIXfkExnYECF4QqJ2SnHxLv3z/99gsfDQrQ6dzN5lZj0=";
    // 生产环境银行公钥
    // private static String publicKey = "BEynMEZOjNpwZIiD9jXtZSGr3Ecpwn7r+m+wtafXHb6VIZTnugfuxhcKASq3hX+KX9JlHODDl9/RDKQv4XLOFak=";
    // 客户私钥，生产需要替换
    private static String privateKey = "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E=";
    // 对称密钥，生产需要替换
    private static String symKey = "VuAzSWQhsoNqzn0K";

    // 企业网银用户号，生产需要替换
    private static String uid = "U006763982";

    private static Random random = new Random();

    private ApiDemo() {
    }

    public static void main(String[] args) throws GeneralSecurityException, IOException, CryptoException, JSONException {
        // 装载BC库,必须在应用的启动类中调用此函数
        Security.addProvider(new BouncyCastleProvider());
        System.setProperty("sun.net.http.retryPost", "false");

        // 业务接口名，这里是查询业务模式接口，生产请替换为对应接口名
        String funcode = "DCLISMOD";
        // 准备接口数据，生产请替换为具体接口请求报文,包含所需的请求字段
        String currentDatetime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String reqid = new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date()) + (BOUND_START + random.nextInt(BOUND_END));
        String data = "{\"request\":{\"body\":{\"buscod\":\"N02030\"},\"head\":{\"funcode\":\"" + funcode + "\",\"userid\":\"" + uid + "\",\"reqid"
                + "\":\"" + reqid + "\"}},\"signature\":{\"sigdat\":\"__signature_sigdat__\",\"sigtim\":\"" + currentDatetime + "\"}}";

        DcHelper dchelper = new DcHelper(url, uid, privateKey, publicKey, symKey);
        //String response = dchelper.sendRequest(data, funcode);

        String data1 = "{\"request\":{\"head\":{\"funcode\":\"NTDMAADD\",\"userid\":\"U006763982\",\"reqid\":\"2024091416424700254437758\"},\"signature\":{\"sigtim\":\"20240914164243\",\"sigdat\":\"__signature_sigdat__\"},\"body\":{\"buscod\":\"N02030\"}}}";

        //String response1 = dchelper.sendRequest(data, funcode);

        JSONObject sendRequestObj = new JSONObject();
        JSONObject requestObj = new JSONObject();
        JSONObject bodyObj = new JSONObject();
        JSONObject headObj = new JSONObject();
        JSONObject signatureObj = new JSONObject();

        bodyObj.put("buscod","N02030");

        headObj.put("funcode","NTDMAADD");
        headObj.put("userid","U006763982");
        headObj.put("reqid","2024091416424700254437758");

        signatureObj.put("sigdat","__signature_sigdat__");
        signatureObj.put("sigtim","20240914164243");

        requestObj.put("body",bodyObj);
        requestObj.put("head",headObj);

        sendRequestObj.put("request",requestObj);
        sendRequestObj.put("signature",signatureObj);

        //String jsonString = sendRequestObj.toJSONString();
        String jsonString = JacksonUtil.getNormalPatternJsonString(sendRequestObj);

        String response1 = dchelper.sendRequest(jsonString, funcode);
        //process("响应报文:\r\n" + response);
    }

    private static void process(String response) {

    }
}
